package com.wontube.smr.web.controller.i18n;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.client.remote.Remoting;
import com.wontube.smr.client.model.vo.SearchCountPreview;
import com.wontube.smr.web.client.IVideoSearchWebHandler;

@Controller
@RequestMapping("/world")
public class I18nSearchController {
	private final static double DB_FACTOR = 0.6;
	private final static int PAGE_SIZE = 30;
	
	@RequestMapping("/results")
	public ModelAndView searchVideos(@RequestParam("search") String query, 
			@RequestParam(value="type", required=false, defaultValue="all") String type){
		if("video".equals(type)){
			type = "all";
		}
		return search_home(type, query);
	}
	
	/*
	@RequestMapping("/movie/q/{query}")
	public ModelAndView searchMovies(@PathVariable("query") String query){
		return search_home("movie", query);
	}
	
	@RequestMapping("/anime/q/{query}")
	public ModelAndView searchTVs(@PathVariable("query") String query){
		return search_home("anime", query);
	}
	
	@RequestMapping("/music/q/{query}")
	public ModelAndView searchMVs(@PathVariable("query") String query){
		return search_home("mv", query);
	}
	
	@RequestMapping("/video/q/{query}")
	public ModelAndView searchAllVideos(@PathVariable("query") String query){
		return search_home("all", query);
	}
	*/
	private ModelAndView search_home(String queryType, String query) {			
		ModelAndView mv = new ModelAndView("i18n/search/search_index");
		Remoting<IVideoSearchWebHandler> remoting = new Remoting<IVideoSearchWebHandler>();
		IVideoSearchWebHandler handler = remoting.createService(IVideoSearchWebHandler.class);
		SearchCountPreview countPreview = handler.getI18nSearchCountPreview(query);		
		mv.addObject("countPreview", countPreview);	
		mv.addObject("query", query);		
		mv.addObject("queryType", queryType);		
		return mv;
	}
	
	/**
	 *  ajax video query
	 * @param type
	 * @param query
	 * @param page
	 * @return
	 */
	@RequestMapping("/video/query")
	public ModelAndView search_v_v(
			@RequestParam("searchType") String type, @RequestParam("query") String query, 
			@RequestParam(value = "page", required=false, defaultValue = "1") Integer page) {
		ModelAndView mv = new ModelAndView("i18n/search/search_list");
		Object items = _search_v_v(query, type, page);
		mv.addObject("searchResult", items);		
		mv.addObject("searchType", type);
		mv.addObject("page", page);
		return mv;
	}

	private Object _search_v_v(String query, String type, Integer page) {
		Remoting<IVideoSearchWebHandler> remoting = new Remoting<IVideoSearchWebHandler>();
		IVideoSearchWebHandler handler = remoting.createService(IVideoSearchWebHandler.class);
		
		if ("all".equals(type)) {
			return handler.searchAllVideosForI18n(DB_FACTOR, query, new PagenateInfo(page, PAGE_SIZE));			
		}

		if ("movie".equals(type)) {
			return handler.searchMoviesForI18n(query, new PagenateInfo(page, PAGE_SIZE));		
		}
		
		if ("trailer".equals(type)) {
			return handler.searchTrailersForI18n(query, new PagenateInfo(page, PAGE_SIZE));		
		}

		if ("anime".equals(type)) {
			return handler.seachAnimesForI18n(query, new PagenateInfo(page, PAGE_SIZE));
		}

		if ("mv".equals(type)) {
			return handler.searchMusicsForI18n(query, new PagenateInfo(page, PAGE_SIZE));		
		}

		if ("others".equals(type)) {
			return handler.searchYoutubeShortFilms(query, new PagenateInfo(page, PAGE_SIZE));		
		}
		
		return null;
	}
}
